﻿using System;
using System.Linq;
using System.Web.Mvc;
using InsightAuto.Domain.Core;
using InsightAuto.Web.ViewModels;
using System.Web.Routing;

namespace InsightAuto.Web.Controllers
{
    public class HomeController : Controller
    {
        private CoreContainer db = new CoreContainer();
        
        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult QuickSearch()
        {
            var groupByFirstPinyin = from m in db.Manufacturers.Include("Brands.Series").ToList()
                                     group m by Utility.FirstPinyinCharOf(m.Name) into g
                                     orderby g.Key
                                     select g;
                                     ;

            
            return PartialView(groupByFirstPinyin);
        }

        private void temp(System.Collections.Generic.IEnumerable<IGrouping<char, Manufacturer>> groupByFirstPinyin)
        {
            throw new NotImplementedException();
        }

        public PartialViewResult NewsPanel(string category = "", int count = 6, bool showTitleBar = true)
        {
            if (count <= 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            var articleCategory = db.ArticleCategories.SingleOrDefault(ac => ac.Name == category);

            NewsPanelVM data = new NewsPanelVM();
            data.ShowTitleBar = showTitleBar;

            if (articleCategory == null)
            {
                data.Articles = db.Articles
                                  .OrderByDescending(a => a.SubmitTime)
                                  .Take(count)
                                  .ToList();
            }
            else
            {
                data.Title = articleCategory.Name;
                data.MoreUrl = UrlHelper.GenerateUrl("Default", "Category", ControllerNames.News, new System.Web.Routing.RouteValueDictionary(new { id = articleCategory.ID }), RouteTable.Routes, this.Request.RequestContext, true);

                data.Articles = db.Articles
                                  .OrderByDescending(a => a.SubmitTime)
                                  .Where(a => a.Category.ID == articleCategory.ID)
                                  .Take(count)
                                  .ToList();
            }

            return PartialView(data);
        }

        public PartialViewResult HomeCenterNewsPanel()
        {
            return PartialView();
        }
    }
}
